package IOperations;

import method.*;
import node.Node;

import java.util.Scanner;

public class Subtraction {
    public static void work() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入被减数整数");
        String minuend = scanner.nextLine();
        if(!isDataSafe.work(minuend)) {
            return;
        }
        System.out.println("请输入减数整数");
        String subtrahend = scanner.nextLine();
        if(!isDataSafe.work(subtrahend)) {
            return;
        }
        //如果被减数与减数相等 直接打印0返回
        if (minuend.equals(subtrahend)) {
            System.out.println(0);
            return;
        }
        Node minuendHead = createNum.work(minuend);
        Node subtrahendHead = createNum.work(subtrahend);
        if (minuendHead.val == 1 && subtrahendHead.val == 0) {
            Addition.work(minuend, subtrahend.substring(1, subtrahend.length()));
        } else if (minuendHead.val == 0 && subtrahendHead.val == 1) {
            Addition.work(minuend, "-" + subtrahend);
        } else {
            //判断为正减正还是负减负
            if (minuendHead.val == 1 && subtrahendHead.val == 1) {
                //判断是大的减小的还是小的减大的
                if (!isBigger.work(minuend, subtrahend)) {
                    Node tem = minuendHead;
                    minuendHead = subtrahendHead;
                    subtrahendHead = tem;
                    minuendHead.val = 0;
                }
            } else if (minuendHead.val == 0 && subtrahendHead.val == 0) {
                minuendHead.val = 0;//结果的符号为被减数的符号，减法过程不关心符号
                subtrahendHead.val = 1;
            }
            //开始减
            Node NodeMinCur = toLastNode.work(minuendHead);
            Node NodeSubCur = toLastNode.work(subtrahendHead);
            while (NodeSubCur != subtrahendHead) {
                if (NodeMinCur.val - NodeSubCur.val < 0) {
                    NodeMinCur.val += 10000;//借位
                    Node temp = NodeMinCur.prev;
                    while (true) {
                        if (temp.val != 0) {
                            temp.val -= 1;
                            break;
                        } else {
                            temp.val += 9999;
                            temp = temp.prev;
                        }
                    }
                }
                NodeMinCur.val = NodeMinCur.val - NodeSubCur.val;
                NodeMinCur = NodeMinCur.prev;
                NodeSubCur = NodeSubCur.prev;
            }
            displayNum.work(minuendHead);
        }
    }

    public static void work(String minuend, String subtrahend) {
        //如果被减数与减数相等 直接打印0返回
        if (minuend.equals(subtrahend)) {
            System.out.println(0);
            return;
        }
        Node minuendHead = createNum.work(minuend);
        Node subtrahendHead = createNum.work(subtrahend);
        if (minuendHead.val == 1 && subtrahendHead.val == 0) {
            Addition.work(minuend, subtrahend.substring(1, subtrahend.length()));
        } else if (minuendHead.val == 0 && subtrahendHead.val == 1) {
            Addition.work(minuend, "-" + subtrahend);
        } else {
            //判断为正减正还是负减负
            if (minuendHead.val == 1 && subtrahendHead.val == 1) {
                //判断是大的减小的还是小的减大的
                if (!isBigger.work(minuend, subtrahend)) {
                    Node tem = minuendHead;
                    minuendHead = subtrahendHead;
                    subtrahendHead = tem;
                    minuendHead.val = 0;
                }
            } else if (minuendHead.val == 0 && subtrahendHead.val == 0) {
                minuendHead.val = 0;//结果的符号为被减数的符号，减法过程不关心符号
                subtrahendHead.val = 1;
            }
            //开始减
            Node NodeMinCur = toLastNode.work(minuendHead);
            Node NodeSubCur = toLastNode.work(subtrahendHead);
            while (NodeSubCur != subtrahendHead) {
                if (NodeMinCur.val - NodeSubCur.val < 0) {
                    NodeMinCur.val += 10000;//借位
                    Node temp = NodeMinCur.prev;
                    while (true) {
                        if (temp.val != 0) {
                            temp.val -= 1;
                            break;
                        } else {
                            temp.val += 9999;
                            temp = temp.prev;
                        }
                    }
                }
                NodeMinCur.val = NodeMinCur.val - NodeSubCur.val;
                NodeMinCur = NodeMinCur.prev;
                NodeSubCur = NodeSubCur.prev;
            }
            displayNum.work(minuendHead);
        }
    }

    public static String divWork(String minuend, String subtrahend) {
        Node minuendHead = createNum.work(minuend);
        Node subtrahendHead = createNum.work(subtrahend);
        //开始减
        Node NodeMinCur = toLastNode.work(minuendHead);
        Node NodeSubCur = toLastNode.work(subtrahendHead);
        while (NodeSubCur != subtrahendHead) {
            if (NodeMinCur.val - NodeSubCur.val < 0) {
                NodeMinCur.val += 10000;//借位
                Node temp = NodeMinCur.prev;
                while (true) {
                    if (temp.val != 0) {
                        temp.val -= 1;
                        break;
                    } else {
                        temp.val += 9999;
                        temp = temp.prev;
                    }
                }
            }
            NodeMinCur.val = NodeMinCur.val - NodeSubCur.val;
            NodeMinCur = NodeMinCur.prev;
            NodeSubCur = NodeSubCur.prev;
        }
        Node cur=minuendHead.next;

        //返回减后的num1
        String ret=new String();
        while(cur!=null) {
            if(cur.prev==minuendHead) {
                ret=ret.concat(String.valueOf(cur.val));
            } else {
                ret=ret.concat(String.format("%04d",cur.val));
            }
            cur=cur.next;
        }
        return ret;
    }
}


